package cn.edu.ctbu.controller.user;

import cn.edu.ctbu.entity.AddressBook;
import cn.edu.ctbu.result.Result;
import cn.edu.ctbu.service.IAddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user/address")
@RestController("用户端地址相关")
public class AddressController {
    @Autowired
    IAddressBookService iAddressBookService;

    /**
     * 获得收货地址
     */
    @GetMapping
    public Result<List<AddressBook>> get(){
        List<AddressBook> list=iAddressBookService.get();
        return Result.success(list);
    }

    /**
     * 修改默认地址
     */
    @PutMapping("/default/{id}")
    public Result putDefault(@PathVariable Long id){
        iAddressBookService.putDefault(id);
        return Result.success();
    }

    /**
     * 获得默认地址
     */
    @GetMapping("/default")
    public Result<AddressBook> getdefault(Long addressId){
        AddressBook book=iAddressBookService.getdefault(addressId);
        return Result.success(book);
    }

    /**
     * 修改地址
     */
    @PutMapping
    public Result put(@RequestBody AddressBook dto){
        iAddressBookService.put(dto);
        return Result.success();
    }
}
